package edu.chinasoft.servlet;

import edu.chinasoft.domain.Dept;
import edu.chinasoft.domain.User;
import edu.chinasoft.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 
 * @author 
 * @date 2020年1月20日:上午11:51:40
 * @version V1.0
 * @Description: 
 * 
*/
@WebServlet(name="myDelectDeptServlet",value="/servlet/delectdeptservlet")
public class DelectDeptServlet extends HttpServlet {
	private static Logger logger = LoggerFactory.getLogger(DelectDeptServlet.class);
	private DeptService deptService;
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.info(" DelectDeptServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		DeptService deptServiceImp = wac.getBean("deptService", DeptService.class);
		this.deptService = deptServiceImp;
		//获取当前商品的id
		String num = request.getParameter("id");
		int id = Integer.parseInt(num);
		Dept dept = new Dept();
		dept.setDeptId(id);
		User user = new User();
		user.getDept().getDeptId();
		if(dept.getDeptId()==user.getDept().getDeptId()) {
			response.sendRedirect(""+request.getContextPath()+"/deletedepterror.jsp");
		}else {
		//调用service的方法删除该商品
		boolean result = deptService.deleteDeptById(dept);
		if(result) {//删除成功
			response.sendRedirect(""+request.getContextPath()+"/servlet/showdeptservlet");
		}
		}
		logger.info(" DelectDeptServlet >doGet end.......");
	}
	
	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.info(" DelectDeptServlet >doPost start.......");
		this.doGet(request, response);
		
		logger.info(" DelectDeptServlet >doPost end.......");
	}
	
}
